
const video = document.getElementById('video');
const playBtn = document.getElementById('play-btn');
const stopBtn = document.getElementById('stop-btn');
const progress = document.getElementById('progress');
const timestamp = document.getElementById('timestamp');


function toggleVideoStatus() {
    if(video.paused) {
        video.play();
    } else {
        video.pause();
    }
}

function updatePlayIcon() {
    if(video.paused) {
        playBtn.innerHTML = '<i class="fas fa-play"></i>';
    } else {
        playBtn.innerHTML = '<i class="fas fa-pause"></i>';
    }
}


function stopVideo() {
    video.currentTime = 0;
    video.pause();
}


function updateProgress() {
    progress.value = (video.currentTime / video.duration) * 100;
    
   
    let mins = Math.floor(video.currentTime / 60);
    if(mins < 10) {
        mins = '0' + String(mins);
    }
    
    let secs = Math.floor(video.currentTime % 60);
    if(secs < 10) {
        secs = '0' + String(secs);
    }
    
    timestamp.innerHTML = `${mins}:${secs}`;
}


function setVideoProgress() {
    video.currentTime = (+progress.value * video.duration) / 100;
}


video.addEventListener('click', toggleVideoStatus);
video.addEventListener('pause', updatePlayIcon);
video.addEventListener('play', updatePlayIcon);
video.addEventListener('timeupdate', updateProgress);

playBtn.addEventListener('click', toggleVideoStatus);
stopBtn.addEventListener('click', stopVideo);
progress.addEventListener('change', setVideoProgress);